package com.huawei.hms.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Pair;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.framework.common.ContainerUtils;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.PackageManagerHelper.PackageStates;
import java.security.cert.X509Certificate;
import java.util.List;

public class a {
    private static a a;
    private static final Object b = new Object();
    private final Context c;
    private final PackageManagerHelper d;
    private String e;
    private String f;

    private a(Context context) {
        this.c = context;
        this.d = new PackageManagerHelper(context);
    }

    public static a a(Context context) {
        synchronized (b) {
            if (a == null) {
                a = new a(context.getApplicationContext());
                a.c();
            }
        }
        return a;
    }

    public PackageStates a() {
        if (TextUtils.isEmpty(this.e) || TextUtils.isEmpty(this.f)) {
            c();
            return this.d.getPackageStates(this.e);
        }
        PackageStates packageStates = this.d.getPackageStates(this.e);
        if (packageStates != PackageStates.ENABLED || this.f.equals(this.d.getPackageSignature(this.e))) {
            return packageStates;
        }
        return PackageStates.NOT_INSTALLED;
    }

    private void c() {
        Pair d = d();
        if (d == null) {
            HMSLog.e("HMSPackageManager", "Failed to find HMS apk");
            return;
        }
        this.e = (String) d.first;
        this.f = (String) d.second;
        HMSLog.i("HMSPackageManager", "Succeed to find HMS apk : " + this.e);
    }

    public String b() {
        return this.e == null ? "com.huawei.hwid" : this.e;
    }

    private Pair<String, String> d() {
        List<ResolveInfo> queryIntentServices = this.c.getPackageManager().queryIntentServices(new Intent(HuaweiApiAvailability.SERVICES_ACTION), DnsConfig.MAX_CACHE_ENTRIES);
        if (queryIntentServices.size() == 0) {
            return null;
        }
        for (ResolveInfo resolveInfo : queryIntentServices) {
            String str = resolveInfo.serviceInfo.applicationInfo.packageName;
            Bundle bundle = resolveInfo.serviceInfo.metaData;
            if (bundle == null) {
                HMSLog.e("HMSPackageManager", "skip package " + str + " for metadata is null");
            } else if (!bundle.containsKey("hms_app_signer")) {
                HMSLog.e("HMSPackageManager", "skip package " + str + " for no signer");
            } else if (bundle.containsKey("hms_app_cert_chain")) {
                String packageSignature = this.d.getPackageSignature(str);
                if (a(str + ContainerUtils.FIELD_DELIMITER + packageSignature, bundle.getString("hms_app_signer"), bundle.getString("hms_app_cert_chain"))) {
                    return new Pair(str, packageSignature);
                }
                HMSLog.e("HMSPackageManager", "checkSinger failed");
            } else {
                HMSLog.e("HMSPackageManager", "skip package " + str + " for no cert chain");
            }
        }
        return null;
    }

    private boolean a(String str, String str2, String str3) {
        if (TextUtils.isEmpty(str2) || TextUtils.isEmpty(str3)) {
            HMSLog.e("HMSPackageManager", "args is invalid");
            return false;
        }
        List b = b.b(str3);
        if (b.size() == 0) {
            HMSLog.e("HMSPackageManager", "certChain is empty");
            return false;
        } else if (b.a(b.a(this.c), b)) {
            X509Certificate x509Certificate = (X509Certificate) b.get(b.size() - 1);
            if (!b.a(x509Certificate, "Huawei CBG HMS")) {
                HMSLog.e("HMSPackageManager", "CN is invalid");
                return false;
            } else if (!b.b(x509Certificate, "Huawei CBG Cloud Security Signer")) {
                HMSLog.e("HMSPackageManager", "OU is invalid");
                return false;
            } else if (b.b(x509Certificate, str, str2)) {
                return true;
            } else {
                HMSLog.e("HMSPackageManager", "signature is invalid: " + str);
                return false;
            }
        } else {
            HMSLog.e("HMSPackageManager", "failed to verify cert chain");
            return false;
        }
    }
}
